<?php

class Controller_Admin_Appearance_Menu_Item_Edit extends Controller
{
	function execute($request)
	{
		$menu_id = $request -> getParam('menu-id');

		$menu = Model::factory('agilet_menu', $menu_id);

		$item_id = $request -> getParam('item-id');
		
		$item = null;
		
		$form = new Form_Admin_Menu_AddElement;
		
		if($item_id){
			$form->removeChild('id');
			$item = $menu -> getItem($item_id);
		}


		if ($item && $request -> isGet())
		{
			$form -> setValues($item);
		}

		if ($request -> isPost() && $form -> isValid($_POST))
		{
			$values = $form -> getValues();

			$menu -> editItem($values, $id);

			$menu -> save();
		}

		$request -> response = View::render(array(
			'form' => $form,
			'menu' => $menu
		), 'admin/edit');
	}

}
